package com.handmark.pulltorefresh.library.internal;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;

import com.handmark.pulltorefresh.library.R;

public class IgottaLoadingSprite extends SurfaceView implements Runnable {

	public static final int MAX_FRAME = 11;

	private static final String TAG = "IgottaLoadingSprite";
	
	private Bitmap mBmpLoading;
	private int mSpriteSize = 0;
	private int mFrame = 0;
	private Thread mThread;
	private boolean mActive = false; 
	
	public IgottaLoadingSprite(Context context) {
		super(context);
		init(null);
	}
	public IgottaLoadingSprite(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}
	public IgottaLoadingSprite(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs);
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		if (!mActive) {
			mActive = true;
	        mThread = new Thread(this);
			mThread.start();
		}
		
		mSpriteSize = mBmpLoading.getHeight();
		Rect rectSrc = new Rect(mFrame * mSpriteSize, 0, (mFrame + 1) * mSpriteSize, mSpriteSize);
		Rect rectTrg = new Rect(0, 0, this.getWidth(), this.getHeight());
		
		Paint paint = new Paint();
	    paint.setAntiAlias(true);
	    paint.setFilterBitmap(true);
	    paint.setDither(true);
	    
	    canvas.drawColor(Color.argb(0, 0, 0, 0));
	    
	    canvas.drawBitmap(mBmpLoading, rectSrc, rectTrg, paint);
	}
	
	@Override
	protected void onVisibilityChanged(View changedView, int visibility) {
		super.onVisibilityChanged(changedView, visibility);
		if (visibility == View.VISIBLE) {
			if (this.getLocalVisibleRect(new Rect(0, 0, 5000, 5000))) {
				if (!mActive) {
					mActive = true;
			        mThread = new Thread(this);
					mThread.start();
				}
			}
		} else {
			mActive = false;
		}
	}
	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		
		
	}
	
	protected void onDetachedFromWindow() {
    	mActive = false;

        super.onDetachedFromWindow();
    }
	
	@Override
	public void run() {	
		Log.i(TAG, "Start Thread");
		while (mActive) {
			try {
				mFrame = (mFrame >= MAX_FRAME) ? 0 : mFrame + 1;
				postInvalidate();
				Thread.sleep(400);				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Log.i(TAG, "Stop Thread");
	}	
		
	/**
	 * Init view
	 * @param attrs
	 */
	private void init(AttributeSet attrs) {
		this.setZOrderOnTop(true);
		getHolder().setFormat(PixelFormat.TRANSPARENT);
		
		mBmpLoading = BitmapFactory.decodeResource(getResources(), R.drawable.sprite_loading);
		
		this.setBackgroundColor(Color.TRANSPARENT);
	}
}
